﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
using System.Windows.Forms;


namespace Marmat.Forms
{
    /// <summary>
    /// 表示 Marmat.Forms.CheckBox。
    /// </summary>
    [DefaultEvent("CheckedChanged")]
    [ToolboxItem(true)]
    [ToolboxBitmap(typeof(System.Windows.Forms.CheckBox))]
    public partial class CheckBox : EditControl
    {
        /// <summary>
        /// 初始化 Marmat.Forms.CheckBox 类的新实例。
        /// </summary>
        public CheckBox()
        {

            // 此调用是设计器所必需的。
            this.InitializeComponent();

            // 在 InitializeComponent() 调用之后添加任何初始化。
            this.BaseEdit = this.checkEdit1;
            this.BindingField.PropertyName = "CheckState";
            this.BorderStyle = EditControlBorderStyles.NoBorder;
        }

        /// <summary>
        /// 获取或设置一个值，该值指示  Marmat.Forms.CheckBox 是否处于选中状态。
        /// </summary>
        /// <value></value>
        /// <returns></returns>
        /// <remarks></remarks>
        [DefaultValue(false)]
        [Category("外观")]
        [Description("获取或设置一个值，该值指示  Marmat.Forms.CheckBox 是否处于选中状态。")]
        public bool Checked
        {
            get
            {
                var result = default(bool);
                if (this.BaseEdit is object && !object.Equals(this.BaseEdit.EditValue, DBNull.Value))
                    result = Convert.ToBoolean(this.BaseEdit.EditValue);
                return result;
            }
            set => this.BaseEdit.EditValue = value;
        }

        /// <summary>
        /// 获取或设置是否显示标题。
        /// </summary>
        [DefaultValue(false)]
        public override bool ShowCaption
        {
            get => base.ShowCaption;
            set => base.ShowCaption = value;
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public new object Value { get; set; }

        /// <summary>
        /// 获取或设置编辑器的边框样式。
        /// </summary>
        [DefaultValue(typeof(BorderStyle), "None")]
        public new EditControlBorderStyles BorderStyle
        {
            get => base.BorderStyle;
            set => base.BorderStyle = value;
        }

        /// <summary>
        /// 获取或设置  Marmat.Forms.CheckBox 的状态。
        /// </summary>
        /// <value></value>
        /// <returns></returns>
        /// <remarks></remarks>
        [DefaultValue(typeof(CheckState), "Unchecked")]
        [Category("外观")]
        [Description("获取或设置  Marmat.Forms.CheckBox 的状态。")]
        public CheckState CheckState
        {
            get => this.checkEdit1.CheckState;
            set => this.checkEdit1.CheckState = value;
        }

        /// <summary>
        /// 与控件关联的文本。
        /// </summary>
        /// <value></value>
        /// <returns></returns>
        /// <remarks></remarks>
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        [Category("外观")]
        [Description("与控件关联的文本。")]
        public override string Text
        {
            get => this.m_Text;
            set
            {
                this.m_Text = value;
                this.BaseEdit.Text = value;
            }
        }
        private string m_Text;

        private void CheckEdit1_CheckedChanged(object sender, EventArgs e)
        {
            CheckedChanged?.Invoke(this, e);
        }

        private void CheckEdit1_CheckStateChanged(object sender, EventArgs e)
        {
            CheckStateChanged?.Invoke(this, e);
        }
    }
}